package org.xdq.demo.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@ComponentScan("org.xdq.demo.service")
@PropertySource("classpath:application.properties")
@MapperScan("org.xdq.demo.dao")//配置在指定的包及其子包下扫描发现映射器，并放入spring容器中管理
@EnableTransactionManagement //启用事务注解管理
public class RootConfig {

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${pagehelper.helper-dialect}")
    private String helperDialect;

    @Value("${pagehelper.reasonable}")
    private String reasonable;


    @Value("${mybatis.mapper-locations}")
    private String mapperLocations;

    @Bean
    public DataSource duridDataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driverClassName);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        PageInterceptor pageInterceptor = new PageInterceptor();

        Properties properties = new Properties();

        properties.setProperty("helperDialect",helperDialect);
        properties.setProperty("reasonable",reasonable);

        pageInterceptor.setProperties(properties);

        sqlSessionFactoryBean.setPlugins(pageInterceptor);

        sqlSessionFactoryBean.setDataSource(ds);


        //创建一个路径模式解析器
        PathMatchingResourcePatternResolver patternResolver
                = new PathMatchingResourcePatternResolver();

        Resource[] resources =
                patternResolver.getResources(
                        mapperLocations);

        //设置映射文件的位置
        sqlSessionFactoryBean.setMapperLocations(
                resources);

        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource ds){
        return new DataSourceTransactionManager(ds);
    }


}
